﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using CThru.Silverlight;
using Core.Infrastructure.AnimationService;
using Core.Infrastructure.Constants;
using Core.Infrastructure.EventArgs;
using Core.Infrastructure.Menu;
using Core.Infrastructure.ScreenManager;
using Core.Infrastructure.Settings;
using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Unity;
using NUnit.Framework;
using TypeMock.ArrangeActAssert;
using Api.Model;

namespace Core.Test
{
    [TestFixture]
    public class ShellViewModelTest
    {
        public ShellViewModel shellViewModel;
        public ShellViewModel fakeShellViewModel;
        public IEventAggregator eventAggregator;
        public IAnimationService animationService;
        public IUnityContainer container;
        public ISettings settings;

        [SetUp, SilverlightSetUp]
        public void SetUpTest()
        {
            eventAggregator = Isolate.Fake.Instance<IEventAggregator>();
            ScreenActivateEvent screenActivateEvent = Isolate.Fake.Instance<ScreenActivateEvent>();
            Isolate.WhenCalled(() => eventAggregator.GetEvent<ScreenActivateEvent>()).DoInstead(context => screenActivateEvent);

            TagsLoadedEvent tagsLoadedEvent = Isolate.Fake.Instance<TagsLoadedEvent>();
            Isolate.WhenCalled(() => tagsLoadedEvent.Subscribe(null)).DoInstead(context =>  null );
            Isolate.WhenCalled(() => eventAggregator.GetEvent<TagsLoadedEvent>()).DoInstead(context => tagsLoadedEvent);

            animationService = Isolate.Fake.Instance<IAnimationService>();
            container = Isolate.Fake.Instance<IUnityContainer>();
            settings = Isolate.Fake.Instance<ISettings>();
            IMenu menu = new Menu();
            shellViewModel = new ShellViewModel(eventAggregator,animationService,container,settings, menu);
        }

        [TearDown]
        public void TearDownTest()
        {
            
        }

        [Test, SilverlightUnitTest]
        public void Constructor_Always_SubscribedToTagLoadedEvent()
        {
            Isolate.Verify.WasCalledWithAnyArguments(() => eventAggregator.GetEvent<TagsLoadedEvent>().Subscribe(null));
        }

        [Test, SilverlightUnitTest]
        public void MenuItemClicked_Null_NoAction()
        {
            shellViewModel.MenuClickCommand.Execute(null);
            Isolate.Verify.WasNotCalled(() => eventAggregator.GetEvent<ScreenActivateEvent>().Publish(null));
        }

        [Test, SilverlightUnitTest]
        public void MenuItemClicked_EventObj_ScreenActivated()
        {
            MenuItem mi = new MenuItem();
            mi.ScreenType = ScreenType.Empty;

            var screenEventArgs = new ScreenEventArgs
            {
                ScreenType = ScreenType.Empty,
                RegionName = RegionConstants.RegionMainArea,
                PerformAnimation = true
            };

            shellViewModel.MenuClickCommand.Execute(mi);
            Isolate.Verify.WasCalledWithExactArguments(
                () => eventAggregator.GetEvent<ScreenActivateEvent>().Publish(screenEventArgs));
        }

        [Test, SilverlightUnitTest]
        public void ShowArticlesByTag_Always_EventCalled()
        {
            var tag = new Tag() { Name = "Rss" };

            var screenEventArgs = new ScreenEventArgs
            {
                ScreenType = ScreenType.ArticlesScreen,
                RegionName = RegionConstants.RegionMainArea,
                PerformAnimation = true,
                NavigationArgs = tag,
            };

            shellViewModel.ShowArticlesByTag(tag);

            Isolate.Verify.WasCalledWithExactArguments(
                () => eventAggregator.GetEvent<ScreenActivateEvent>().Publish(screenEventArgs));
        }

        [Test, SilverlightUnitTest]
        public void OnLoad_AnyParam_ActiveFlagSet()
        {
            shellViewModel.Loaded.Execute(null);
            Assert.IsTrue(shellViewModel.IsActive);
        }

        [Test, SilverlightUnitTest]
        public void OnUnload_AnyParam_ActiveFlagSet()
        {
            shellViewModel.Unloaded.Execute(null);
            Assert.IsFalse(shellViewModel.IsActive);
        }
    }
}
